package com.easyctba.core.service.impl;

import com.easyctba.common.core.domain.R;
import com.easyctba.common.core.utils.MapstructUtils;
import com.easyctba.common.core.utils.StringUtils;
import com.easyctba.common.mybatis.core.page.TableDataInfo;
import com.easyctba.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.easyctba.core.domain.bo.CtbaFiasPolylineBo;
import com.easyctba.core.domain.vo.CtbaFiasPolylineVo;
import com.easyctba.core.domain.CtbaFiasPolyline;
import com.easyctba.core.mapper.CtbaFiasPolylineMapper;
import com.easyctba.core.service.ICtbaFiasPolylineService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * FIAS的折线图分析Service业务层处理
 *
 * @author Huang
 * @date 2024-04-03
 */
@RequiredArgsConstructor
@Service
public class CtbaFiasPolylineServiceImpl implements ICtbaFiasPolylineService {

    private final CtbaFiasPolylineMapper baseMapper;

    /**
     * 查询FIAS的折线图分析
     */
    @Override
    public CtbaFiasPolylineVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询FIAS的折线图分析列表
     */
    @Override
    public TableDataInfo<CtbaFiasPolylineVo> queryPageList(CtbaFiasPolylineBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CtbaFiasPolyline> lqw = buildQueryWrapper(bo);
        Page<CtbaFiasPolylineVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询FIAS的折线图分析列表
     */
    @Override
    public List<CtbaFiasPolylineVo> queryList(CtbaFiasPolylineBo bo) {
        LambdaQueryWrapper<CtbaFiasPolyline> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CtbaFiasPolyline> buildQueryWrapper(CtbaFiasPolylineBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CtbaFiasPolyline> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getFiasId() != null, CtbaFiasPolyline::getFiasId, bo.getFiasId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), CtbaFiasPolyline::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增FIAS的折线图分析
     */
    @Override
    public Boolean insertByBo(CtbaFiasPolylineBo bo) {
        CtbaFiasPolyline add = MapstructUtils.convert(bo, CtbaFiasPolyline.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改FIAS的折线图分析
     */
    @Override
    public Boolean updateByBo(CtbaFiasPolylineBo bo) {
        CtbaFiasPolyline update = MapstructUtils.convert(bo, CtbaFiasPolyline.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CtbaFiasPolyline entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除FIAS的折线图分析
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    /**
     * 新增折线
     * @param ctbaFiasPolyline 折线对象
     * @return 是否成功
     */
    @Override
    public Boolean addPolyline(CtbaFiasPolyline ctbaFiasPolyline) {
        int insert = baseMapper.insert(ctbaFiasPolyline);
        return insert >= 1;
    }

    /**
     * 检查折线名是否重复
     * @param name 折线名
     * @param id fias id
     * @return bool
     */
    @Override
    public Boolean checkName(String name, Long id) {
        LambdaQueryWrapper<CtbaFiasPolyline> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CtbaFiasPolyline::getFiasId,id).eq(CtbaFiasPolyline::getName,name);
        List<CtbaFiasPolyline> ctbaFiasPolylines = baseMapper.selectList(wrapper);
        return ctbaFiasPolylines.isEmpty();
    }

    @Override
    public R<List<CtbaFiasPolyline>> getInfo(Long id) {
        LambdaQueryWrapper<CtbaFiasPolyline> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CtbaFiasPolyline::getFiasId,id);
        return R.ok(baseMapper.selectList(wrapper));
    }

    @Override
    public R<Void> delete(Long id) {
        return baseMapper.deleteById(id)>0?R.ok():R.fail("删除失败");
    }

    /**
     * 获取折线图信息
     * @param id fias id
     * @return 折线图对象列表
     */
    public List<CtbaFiasPolyline> getPolyline(Long id) {
        LambdaQueryWrapper<CtbaFiasPolyline> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(id!=null,CtbaFiasPolyline::getFiasId,id);
        return baseMapper.selectList(wrapper);
    }
}
